package com.hzsk.lisence.controller;

import com.hzsk.lisence.entity.HardwareEntity;
import com.hzsk.lisence.entity.HardwareGroupingEntity;
import com.hzsk.lisence.entity.query.LisenceServiceQuery;
import com.hzsk.lisence.entity.response.IncResponce;
import com.hzsk.lisence.service.HardwareService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@CrossOrigin
@RestController
@RequestMapping("/api/lisence/hardware/")
public class HardwareController {

    @Resource
    public HardwareService hardwareService;


    @PostMapping("createhardware")
    @ApiOperation(value = "创建硬件信息")
    public IncResponce createHardware(@RequestBody HardwareEntity hardwareEntity){
        IncResponce incResponce = new IncResponce();
        try {
          synchronized (this) {
              hardwareService.createHardware(hardwareEntity,hardwareEntity.getOperatorUserId(),incResponce);
          }
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }



    @PutMapping("updatehardware")
    @ApiOperation(value = "修改硬件信息")
    public IncResponce updateHardware(@RequestBody HardwareEntity hardwareEntity){
        IncResponce incResponce = new IncResponce();
        try {
            hardwareService.updateHardware(hardwareEntity,hardwareEntity.getOperatorUserId(),incResponce);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @DeleteMapping("delhardware")
    @ApiOperation(value = "删除硬件信息")
    public IncResponce delHardware(@RequestParam("id")Integer id,@RequestParam("operatorUserId")Integer operatorUserId){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(hardwareService.delHardware(id,operatorUserId));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }




    @GetMapping("setsoftwareconngrouping")
    @ApiOperation(value = "设置硬件分组关联硬件")
    public IncResponce setSoftwareConnGrouping(@RequestParam("id")Integer id,@RequestParam int[] hardwareIds){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(hardwareService.setSoftwareConnGrouping(id,hardwareIds));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }

    @PostMapping("createhardwaregrouping")
    @ApiOperation(value = "创建硬件分组信息")
    public IncResponce createHardwareGrouping(@RequestBody HardwareGroupingEntity hardwareGroupingEntity){
        IncResponce incResponce = new IncResponce();
        try {
            hardwareService.createHardwareGrouping(hardwareGroupingEntity,hardwareGroupingEntity.getOperatorUserId(),incResponce);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @GetMapping("selecthardwaregrouping")
    @ApiOperation(value = "查询硬件分组信息")
    public IncResponce selectHardwareGrouping(){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(hardwareService.selectHardwareGrouping());
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @PostMapping("selecthardwaregroupinginfo")
    @ApiOperation(value = "根据硬件分组查询所有硬件信息")
    public IncResponce selectHardwareGroupingInfo(@RequestBody LisenceServiceQuery lisenceServiceQuery){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(hardwareService.selectHardwareGroupingInfo(lisenceServiceQuery));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @PostMapping("selecthardwaretobegrouped")
    @ApiOperation(value = "根据组id查询待分组信息")
    public IncResponce selectHardwareToBeGrouped(@RequestBody LisenceServiceQuery lisenceServiceQuery){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(hardwareService.selectHardwareToBeGrouped(lisenceServiceQuery));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @PutMapping("updatehardwaregrouping")
    @ApiOperation(value = "修改硬件分组信息")
    public IncResponce updateHardwareGrouping(@RequestBody HardwareGroupingEntity hardwareGroupingEntity){
        IncResponce incResponce = new IncResponce();
        try {
            hardwareService.updateHardwareGrouping(hardwareGroupingEntity,hardwareGroupingEntity.getOperatorUserId(),incResponce);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @DeleteMapping("delhardwaregrouping")
    @ApiOperation(value = "删除硬件分组信息")
    public IncResponce delHardwareGrouping(@RequestParam("id")Integer id,@RequestParam("operatorUserId")Integer operatorUserId){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(hardwareService.delHardwareGrouping(id,operatorUserId));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }




    @PostMapping("selecthardwarebyuserid")
    @ApiOperation(value = "查询硬件信息")
    public IncResponce selectHardwareByUserId(@RequestBody LisenceServiceQuery lisenceServiceQuery){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(hardwareService.selectHardwareByUserId(lisenceServiceQuery));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @GetMapping("selecthardwarenamebykey")
    @ApiOperation(value = "查询硬件信息根据key")
    public IncResponce selectHardwareNameByKey(@RequestParam("key")String key){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(hardwareService.selectHardwareNameByKey(key));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }



    @GetMapping("selectsoftwareinfobyid")
    @ApiOperation(value = "根据硬件id查询关联软件信息")
    public IncResponce selectSoftwareInfoById(@RequestParam("hardwareId")Integer hardwareId){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(hardwareService.selectSoftwareInfoById(hardwareId));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }









}
